package cn.jervain.nut.auth.core.storage;

import cn.jervain.nut.auth.core.exception.AuthException;
import cn.jervain.nut.auth.core.token.Token;
import cn.jervain.nut.auth.core.util.Assert;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.HashMap;
import java.util.Map;

public class InMemoryTokenStorage<T> implements TokenStorage<T> {

    private Map<String, Token> storage = new HashMap<>();

    @Override
    public String put(Token token) {
        Assert.notNull(token);
        String sign = RandomStringUtils.randomAlphanumeric(8);
        storage.put(sign, token);
        return sign;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Token<T> get(String sign) throws AuthException {
        Token<T> token = (Token<T>) storage.get(sign);
        Assert.notNull(token);
        return token;
    }

}
